from flask import Flask, request
from flask_cors import CORS


def create_app():
    app = Flask(__name__)
    CORS(app)
    # 注册蓝图

    from app.blueprints.smt1_laser_api import smt1laser_bp
    app.register_blueprint(smt1laser_bp, url_prefix='/api/smt1laser')

    from app.blueprints.smt2_laser_api import smt2laser_bp
    app.register_blueprint(smt2laser_bp, url_prefix='/api/smt2laser')

    from app.blueprints.auxiliary_fixtures_api import auxiliary_fixtures_bp
    app.register_blueprint(auxiliary_fixtures_bp, url_prefix='/api/auxiliary_fixtures')

    from app.blueprints.smt_api import smt_bp
    app.register_blueprint(smt_bp, url_prefix='/api/smt')

    from app.blueprints.material_api import material_bp
    app.register_blueprint(material_bp, url_prefix='/api/material')

    from app.blueprints.smt1_spi_api import smt1_spi_bp
    app.register_blueprint(smt1_spi_bp)

    from app.blueprints.smt2_spi_api import smt2_spi_bp
    app.register_blueprint(smt2_spi_bp)

    from app.blueprints.smt2_reflow_soldering_api import smt2_reflow_soldering_bp
    app.register_blueprint(smt2_reflow_soldering_bp)

    from app.blueprints.smt1_aoi_api import smt1_aoi_bp
    app.register_blueprint(smt1_aoi_bp)

    from app.blueprints.smt2_aoi_api import smt2_aoi_bp
    app.register_blueprint(smt2_aoi_bp)

    from app.blueprints.fixtures_receive_api import fixtures_receive_api
    app.register_blueprint(fixtures_receive_api)

    from app.blueprints.smt_spi_data_analysis import smt_spi_analysis_bp
    app.register_blueprint(smt_spi_analysis_bp)

    from app.blueprints.aoi_scan_api import aoi_scan_bp
    app.register_blueprint(aoi_scan_bp, url_prefix='/api/aoi_scan')

    from app.blueprints.automation_eol_api import automation_eol_bp
    app.register_blueprint(automation_eol_bp, url_prefix='/api/automation_eol')

    # 兼容无前缀的旧地址：/list_files 与 /file_detail
    from app.blueprints.automation_eol_api import list_files as eol_list_files, file_detail as eol_file_detail
    app.add_url_rule('/list_files', view_func=eol_list_files, methods=['GET', 'OPTIONS'], endpoint='eol_list_files_no_prefix')
    app.add_url_rule('/file_detail', view_func=eol_file_detail, methods=['GET', 'OPTIONS'], endpoint='eol_file_detail_no_prefix')

    # 新增组装EOL拦截接口
    from app.blueprints.assembly_eol_api import assembly_eol_bp
    app.register_blueprint(assembly_eol_bp, url_prefix='/api/assembly_eol')

    return app
